Skip to content

craft-xls: read RC4 and RC4 CryptoAPI encrypted workbooks (ports of gridcraft#847, #3713) - #1018

Merged
dylanmtaylor merged 7 commits into
mainfrom
craft-xls-crypt
Nov 12, 2026
Merged

dylanmtaylor merged 7 commits into
mainfrom
craft-xls-crypt

Conversation

@dylanmtaylor

Copy link
Copy Markdown
Member

Stacked on #1005 (on #999, on #956). Review the last two commits only.

Ported from storytold/gridcraft#847 by @itrycodinganditsbad (RC4, commit author Fabio Lussi) and storytold/gridcraft#3713 by @giuseppeps (RC4 CryptoAPI, which builds on #847; commit author giuseppeps).

What

craft-xls reads password-encrypted workbooks ([MS-XLS] 2.2.10, [MS-OFFCRYPTO] 2.3.5, 2.3.6):

  • RC4 (FilePass version 1.1, keyed through MD5) and RC4 CryptoAPI (versions 2.2, 3.2 and 4.2, keyed through SHA-1 with a 40- to 128-bit key, a 40-bit key padded to 128 bits). CryptoAPI is what current Excel writes.
  • The record data after the FilePass is decrypted in place, rekeyed every 1024 bytes of the stream. Record headers, BOF, FilePass and the other clear records, and BoundSheet8's stream position stay as they are. The FilePass stays in place, so substream offsets don't move.
  • read_with_password(bytes, limits, password) and is_encrypted(bytes). A workbook with no password reads the same whatever password is given.
  • New errors: Error::WrongPassword and Error::UnsupportedEncryption(name) (XOR obfuscation; CryptoAPI with AES, another cipher or hash, an external provider, or a key outside 40 to 128 bits). A damaged FilePass is Error::Invalid. Every size in it is checked before use.
  • craft_xls::testing::{encrypt_rc4, encrypt_cryptoapi, cryptoapi_filepass} build encrypted fixtures.

Where it lives, and what changed from the GridCraft PRs

  • In craft-xls, not craft-offcrypto. The scheme here is BIFF8's record-level stream encryption: which records stay clear, and block numbers by stream offset. craft-offcrypto handles encrypted OOXML packages (EncryptionInfo/EncryptedPackage), which this shares no code with. If another binary Office format needs RC4 CryptoAPI later, the key derivation can move then.
  • Excel's default password (VelvetSweatshop) is now tried by read/read_with itself, as Excel does, instead of by GridCraft's engine. So Error::Encrypted means a password is really needed, and every app gets the same behaviour.
  • A FilePass record cut too short to hold its scheme is now Error::Invalid. Before craft-dxf: R12 binary, more code pages, borrowing sections, exponent reals (ports of open CadCraft PRs) #847 every FilePass was Error::Encrypted. One existing test was updated for this.
  • The test-only encryptors are written without unwrap or indexing, since craft_xls::testing is always compiled here.
  • GridCraft's engine and docs changes (the password prompt, finish_open) stay in GridCraft.

Checked (own target dir)

  • cargo fmt --check -p craft-xls, cargo clippy -p craft-xls --all-targets -- -D warnings: clean, at both commits.
  • cargo test -p craft-xls: all pass, at both commits (tests/read.rs: 21 after the RC4 commit, 23 after CryptoAPI). Ported tests:
    • RC4: right and wrong password, a 500-row sheet across block boundaries, named unsupported schemes, a damaged FilePass;
    • Excel's default password opening without one (new);
    • CryptoAPI with 128-, 40- and 56-bit keys;
    • every FilePass field and every cut length of the CryptoAPI record.
  • The test encryptor reuses the decrypt code, so these tests only show the reader and writer agree. The GridCraft PR author checked real files from other tools (msoffcrypto-tool's rc4cryptoapi_password.xls, Apache POI's 35897-type4.xls and password.xls). I did not re-run that here, and no third-party file is committed.

The format part of gridcraft#847 and #3713 is superseded by this. What remains in GridCraft is the password prompt in the engine, which calls craft_xls::read_with_password, and its docs.

dylanmtaylor and others added 6 commits November 11, 2026 23:49
…t formats share

craft-xls carried its own CellRef, Range, Value, CellError, Visibility, sheet-name
quoting, built-in number format table and indexed palette. craft-xlsx and craft-ods
need the same ones, so they move to a crate with no dependencies and craft-xls
re-exports them: an application gets one Value and one CellRef whichever file it read.

Added on the way: A1 parsing (CellRef::parse, Range::parse), Range::count and a1,
CellError::parse and the newer error values SpreadsheetML stores as text,
builtin_format_id for writers.
Each Dv after a DVal becomes a DataValidation in SpreadsheetML's terms: type,
operator, formulas as A1 text from the top left cell of all the rule's ranges (a
literal list quoted, items separated by commas), the four messages, alert style
and flags. Exactly the DVal's count of rules is read, as Excel does; a rule Excel
would refuse is skipped with a warning, and the rest are kept. A sheet keeps at
most 65,536 rules (the count was not capped on its own).

Ported from storytold/gridcraft#3636.
…ilter range

- Notes with their TxO text, author and visibility.
- CondFmt and CF records (cell value is, formula is), formulas from the bounding
  range's top left cell, and the DXFN as a DifferentialFormat whose unset parts are
  None.
- The drawing group's BLIP store (PNG, JPEG, EMF, WMF with its placeable header,
  DIB as BMP) and each sheet's picture shapes with their client anchors.
- The AutoFilter range from AutoFilterInfo and _FilterDatabase.

From the review of the source PR: picture bytes are shared by the shapes that show
them, pictures, comments and rules are capped per sheet, inflated metafiles share
one allowance, damaged object and CF records are skipped with a warning instead of
failing the read, and CF formulas are relative to the CondFmt's bounding range.
ValidationOperator is now Operator, shared with conditional formats.

Ported from storytold/gridcraft#3457.
A FilePass record with RC4 (version 1.1) is checked against the password through
its MD5 verifier, and the record data after it is decrypted, rekeyed every 1024
bytes of the stream; record headers and the few records Excel leaves in the clear
are kept. read_with_password takes the password. Without one, Excel's default
password (VelvetSweatshop) is tried, as Excel opens a workbook protected only
against changes, and Error::Encrypted says a password is needed. XOR obfuscation
and RC4 CryptoAPI are Error::UnsupportedEncryption; a wrong password is
Error::WrongPassword.

Ported from storytold/gridcraft#847.
What current Excel writes for an .xls saved with a password (FilePass version 2.2,
3.2 or 4.2): H0 = SHA-1(salt, UTF-16LE password), each 1024-byte block keyed by
SHA-1(H0, block number) cut to the key size, a 40-bit key padded with zeros to 128
bits, and the password checked against the verifier's SHA-1. Every size in the
FilePass record is checked before use; CryptoAPI with AES, another cipher or hash,
an external provider or a key outside 40 to 128 bits is named as unsupported.

Ported from storytold/gridcraft#3713.
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 20 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 45ba44f and ef2ec56.

Files ignored due to path filters: 1

  • Cargo.lock is excluded by !**/*.lock
Files selected for processing: 7

  • crates/xls/Cargo.toml
  • crates/xls/README.md
  • crates/xls/src/crypt.rs
  • crates/xls/src/lib.rs
  • crates/xls/src/read.rs
  • crates/xls/src/testing.rs
  • crates/xls/tests/read.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Comment thread crates/xls/src/crypt.rs
let pw: Vec<u8> = password.encode_utf16().flat_map(u16::to_le_bytes).collect();
let h0 = md5(&[&pw]);
let truncated = h0.get(..5).unwrap_or_default();
let mut m = Md5::new();
Comment thread crates/xls/src/crypt.rs
}

fn md5(parts: &[&[u8]]) -> [u8; 16] {
let mut m = Md5::new();
Comment thread crates/xls/src/crypt.rs
}

fn sha1(parts: &[&[u8]]) -> [u8; SHA1_LEN] {
let mut m = Sha1::new();
Comment thread crates/xls/src/crypt.rs
let verifier: [u8; 16] = std::array::from_fn(|n| n as u8 + 0x60);
let pw: Vec<u8> = password.encode_utf16().flat_map(u16::to_le_bytes).collect();
let h0 = md5(&[&pw]);
let mut m = Md5::new();
@dylanmtaylor
dylanmtaylor changed the base branch from craft-xls-objects to main November 12, 2026 01:53
@dylanmtaylor
dylanmtaylor merged commit cabcf2f into main Nov 12, 2026
67 checks passed
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
D Security Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants